<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4213 2010-08-02 03:35:10Z leha $
 * @since		2.0.0
 */
?>
<?php
$this->headTitle($this->translator('banner_list_page_title'));

$this->headScript()
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate.min.js');
?>

<div id="r-heading">
    <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/banner.png'; ?>" alt=""/>
    <span><?php echo $this->translator('banner_list_breadcrumb'); ?></span>
    <input type="button" id="btnCreate" class="button" value="<?php echo $this->translate('banner_add_breadcrumb'); ?>" onclick="location = '<?php echo $this->url(array(), 'ad_banner_add'); ?>';"/>
</div>


<!-- Search form -->
<form method="post" action="<?php echo $this->url(array(), 'ad_banner_list'); ?>" id="searchBannerForm">
    <div id="filters">
        <div class="f-data">
            <label><?php echo $this->translator('banner_list_search_keyword'); ?>:</label>
            <input type="text" style="width: 180px" name="keyword" value="<?php if (isset($this->exp['keyword'])) : ?><?php echo $this->exp['keyword']; ?><?php endif; ?>" />
        </div>

        <div class="f-data">
            <label><?php echo $this->translator('banner_list_search_id'); ?>:</label>
            <input type="text" style="width: 100px" name="bannerId" id="bannerId" value="<?php if (isset($this->exp['id'])) : ?><?php echo $this->exp['id']; ?><?php endif; ?>" />
        </div>
        <div class="f-data">
            <label><?php echo $this->translator('banner_list_search_page'); ?>:</label>
            <select name="page" style="width: 180px">
                <option value="">---</option>
                <?php for ($i = 0; $i < count($this->pages); $i++) : ?>
                    <option value="<?php echo $this->pages[$i]->route; ?>"<?php if (isset($this->exp['route']) && $this->exp['route'] == $this->pages[$i]->route) : ?> selected="selected"<?php endif; ?>><?php echo $this->pages[$i]->title; ?></option>
                <?php endfor; ?>
            </select>
        </div>	
        <div class="f-data">
            <label><?php echo $this->translator('banner_list_search_status'); ?>:</label>
            <select name="status" style="width: 180px">
                <option value=""<?php if (!isset($this->exp['status'])) : ?> selected="selected"<?php endif; ?>>---</option>
                <option value="active"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'active') : ?> selected="selected"<?php endif; ?>><?php echo $this->translator('banner_list_status_activated'); ?></option>
                <option value="inactive"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'inactive') : ?> selected="selected"<?php endif; ?>><?php echo $this->translator('banner_list_status_not_activated'); ?></option>
            </select>
        </div>

        <div class="f-submit">
            <button class="button" type="submit"><span><?php echo $this->translator('banner_list_search_button'); ?></span></button>
        </div>
    </div>
</form>

<!-- List of banners -->
<table class="list" cellspacing="0">
    <thead>
        <tr>
            <td width="3%">
                <input type="checkbox" class="chkAll"/>
            </td>
            <td width="20%">
                <?php echo $this->translator('banner_name'); ?>
            </td>
            <td width="15%">
                <?php echo $this->translator('banner_list_actived_date'); ?>
            </td>
            <td width="10%">
                <?php echo $this->translator('banner_status'); ?>
            </td>
            <td width="35%">
                <?php echo $this->translator('banner_image_url'); ?>
            </td>
            <td width="15%">
                <?php echo $this->translate('banner_list_actions'); ?>
            </td>
        </tr>
    </thead>
    <tbody>
        <?php if ($this->numBanners == 0) : ?>
            <tr><td colspan="5"><?php echo $this->translator('banner_list_not_found'); ?></td></tr>	
        <?php else : ?>
            <?php foreach ($this->banners as $banner) : ?>
                <tr id="bannerRow_<?php echo $banner->id; ?>">
                    <td>
                        <input type="checkbox" class="chkId" value="<?php echo $banner->id; ?>"/>
                    </td>
                    <td><?php echo $banner->name; ?></td>
                    <td><?php echo date('Y-m-d H:i:s', strtotime($banner->created_date)); ?></td>
                    <td id="bannerStatus_<?php echo $banner->id; ?>">
                        <?php if ($banner->status == 'active') : ?>
                            <?php echo $this->translator('banner_list_status_activated'); ?>
                        <?php elseif ($banner->status == 'inactive') : ?>
                            <?php echo $this->translator('banner_list_status_not_activated'); ?>
                        <?php else : ?>
                            &nbsp;
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if (null != $banner->image_url) : ?> 
                            <?php if ('.swf' == substr($banner->image_url, strlen($banner->image_url) - 4, 4)) : ?> 
                                <object width="100" height="50">
                                    <param name="movie" value="<?php echo $banner->image_url; ?>">
                                    <embed src="<?php echo $banner->image_url; ?>" width="100" height="50"></embed>
                                </object>	
                            <?php else: ?>
                                <img src="<?php echo $banner->image_url; ?>" width="100" height="50" />
                            <?php endif; ?>
                        <?php else : ?>
                            &nbsp;
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($this->allow('edit')) : ?>
                            <a href="<?php echo $this->url(array('id' => $banner->id), 'ad_banner_edit'); ?>/"><?php echo $this->translator('banner_list_action_edit'); ?></a> |
                        <?php endif; ?>

                        <?php if ($this->allow('delete')) : ?> 
                            <a class="deleteAction" rel="<?php echo $banner->id; ?>" href="javascript: void(0);"><?php echo $this->translator('banner_list_action_delete'); ?></a> |
                        <?php endif; ?>

                        <?php if ($this->allow('activate')) : ?>
                            <a class="activateAction" rel="<?php echo $banner->id; ?>_<?php echo $banner->status; ?>" href=""><?php if ($banner->status == 'active') : ?><?php echo $this->translator('banner_list_action_deactivate'); ?><?php else : ?><?php echo $this->translator('banner_list_action_activate'); ?><?php endif; ?></a>
                        <?php endif; ?>
                    </td>

                </tr>
            <?php endforeach; ?>
        <?php endif; ?>

    </tbody>
    <tfoot>
        <tr>
            <td>
                <input type="checkbox" class="chkAll"/>
            </td>
            <td>
                <?php echo $this->translator('banner_name'); ?>
            </td>
            <td>
                <?php echo $this->translator('banner_list_actived_date'); ?>
            </td>
            <td>
                <?php echo $this->translator('banner_status'); ?>
            </td>
            <td>
                <?php echo $this->translator('banner_image_url'); ?>
            </td>
            <td >
                <?php echo $this->translate('banner_list_actions'); ?>
            </td>
        </tr>
    </tfoot>
</table>
<?php if ($this->numberBanner > 0 ){?>
<div id="pagination">
    <?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
</div>
<?php }?>

<script type="text/javascript">
    $(document).ready(function() {
        <?php if ($this->allow('delete')) : ?>
        $('a.deleteAction').click(function() {
            var self = this;
            if(confirm('<?php echo addslashes($this->translator('banner_list_action_delete_confirm')); ?>')){
                $(self).html('').addClass('loading');
                var id = $(self).attr('rel');
                $.ajax({
                    type: 'POST',
                    url: '<?php echo $this->url(array(), 'ad_banner_delete'); ?>',
                    data: { id: id },
                    success: function(response) {
                        if ('RESULT_OK' == response) {
                            $('#bannerRow_' + id).fadeOut('slow');
                        }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(xhr.status);
                        alert(xhr.statusText);
                        alert(thrownError);
                    }
                });
            }

        });
        <?php endif; ?>

        <?php  if ($this->allow('activate')) : ?>
        $('a.activateAction').click(function() {
            var self = this;
            var arr = $(this).attr('rel').split('_');
            
            $.ajax({
                url: '<?php echo $this->url(array(), 'ad_banner_activate')?>',
                type: 'post',
                beforeSend: function(){$(self).html('').addClass('loading');},
                data: { id: arr[0], status: arr[1] },
                success: function(response) {
                    $(self).attr('rel', arr[0] + '_' + response);
                    if (response == 'active') {
                        $('#bannerStatus_' + arr[0]).html("<?php echo $this->translator('banner_list_status_activated'); ?>");
                        $(self).html("<?php echo $this->translator('banner_list_action_deactivate'); ?>");
                    } else {
                        $('#bannerStatus_' + arr[0]).html("<?php echo $this->translator('banner_list_status_not_activated'); ?>");
                        $(self).html("<?php echo $this->translator('banner_list_action_activate'); ?>");
                    }
                    $(self).removeClass('loading');
                    
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(xhr.statusText);
                    alert(thrownError);
                }

            });
            
            return false;
        });
        <?php endif; ?>

                    $('#searchBannerForm').validate({
                        rules: {		
                            bannerId: {
                                number: true
                            }
                        },
                        messages: {
                            bannerId: {
                                number: "<?php echo $this->translator('banner_list_id_invalid'); ?>"
                            }
                        }
                    });
                });
    </script>
